package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CommentMapper;
import com.itheima.dao.CommentReportMapper;
import com.itheima.dao.FileMapper;
import com.itheima.dao.UserMapper;
import com.itheima.pojo.Comment;
import com.itheima.pojo.CommentReport;
import com.itheima.pojo.File;
import com.itheima.pojo.User;
import com.itheima.service.CommentService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private FileMapper fileMapper;

    @Resource
    private IdWorker idWorker;

    @Autowired
    private CommentReportMapper commentReportMapper;

    /**
     * 增加评论
     *
     * @Param Comment
     */
    @Override
    public Integer add(Comment comment) {
        comment.setUid(String.valueOf(idWorker.nextId()));
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        return commentMapper.insertSelective(comment);
    }

    /**
     * 删除评论
     *
     * @Param Comment
     */
    @Override
    public Integer delete(Comment comment) {
        return commentMapper.deleteByPrimaryKey(comment);
    }

    /**
     * 编辑评论
     *
     * @Param Comment
     */
    @Override
    public Integer edit(Comment comment) {
        comment.setUpdateTime(new Date());
        return commentMapper.updateByPrimaryKeySelective(comment);
    }

    /**
     * 获取评论列表
     *
     * @Param Comment
     */
    @Override
    public Page<Comment> getList(Comment comment) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if (comment.getType() != null) {
            criteria.andEqualTo("type",comment.getType());
        }
        if (!comment.getSource().equalsIgnoreCase("all")) {
            criteria.andEqualTo("source", comment.getSource());
        }
        if (comment.getUserName() != null && !comment.getUserName().equals("")) {
            User user = new User();
            user.setUserName(comment.getUserName());
            User user1 = userMapper.selectOne(user);
            if (user1 != null) {
                criteria.andEqualTo("userUid", user1.getUid());
            }
        }
        if (comment.getKeyword() != null && !comment.getKeyword().equals("")) {
            criteria.andLike("content", "%"+comment.getKeyword()+"%");
        }

        PageHelper.startPage(comment.getCurrentPage(), comment.getPageSize());
        List<Comment> commentList = commentMapper.selectByExample(example);
        if (commentList.size() > 0) {
            for (Comment comment1 : commentList) {

                if(comment1.getSource().equalsIgnoreCase("MESSAGE_BOARD")){
                    comment1.setSourceName("留言板");
                }
                if(comment1.getSource().equalsIgnoreCase("ABOUT")){
                    comment1.setSourceName("关于我");
                }
                if(comment1.getSource().equalsIgnoreCase("BLOG_INFO")){
                    comment1.setSourceName("博客详情");
                }

                User user = userMapper.selectByPrimaryKey(comment1.getUserUid());
                if (null != user) {
                    File file = fileMapper.selectByPrimaryKey(user.getAvatar());
                    if (null != file) {
                        user.setPhotoUrl(file.getPicUrl());
                    }
                    comment1.setUser(user);
                }
            }
        }
        return (Page<Comment>) commentList;

    }


    /**
     * 关闭评论
     *
     * @param userUid
     * @return
     */
    @Override
    public void closeEmailNotification(String userUid) {


        userMapper.closeEmailNotification(userUid);
    }

    /**
     * 获取评论列表和回复
     *
     * @param user
     * @return
     */
    @Override
    public Comment getListByUser(User user) {
        return null;
    }

    /**
     * 举报
     *
     * @param comment
     * @param username
     * @return
     */
    @Override
    public Integer report(Comment comment, String username, String content) {

        CommentReport report = new CommentReport();
        report.setUid(String.valueOf(idWorker.nextId()));
        User user = new User();
        user.setUserName(username);
        User user1 = userMapper.selectOne(user);
        report.setUserUid(user1.getUid());
        report.setReportCommentUid(comment.getUid());
        report.setReportUserUid(comment.getUserUid());
        report.setContent(content);
        report.setProgress(0);
        report.setStatus(0);
        report.setCreateTime(new Date());
        report.setUpdateTime(new Date());
        return commentReportMapper.insert(report);
    }

    @Override
    public int findCountByDay(String day) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("createTime", day + "%");
        return commentMapper.selectCountByExample(example);
    }
}
